package com.sszh.base.controller;

import com.google.gson.Gson;
import com.google.common.reflect.TypeToken;
import com.sszh.base.controller.inf.UserControllerInf;
import com.sszh.base.controller.model.*;
import com.sszh.base.service.UserService;
import com.sszh.base.utils.Constant;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author WangJianGuo
 * @Title: UserController
 * @Package com.sszh.base.controller
 * @Description: 控制器
 * @date 2018/3/53:21
 */
@Log4j
@RestController
@RequestMapping("/sszh/base")
public class UserController implements UserControllerInf {

    @Autowired
    UserService userService;

    @Override
    @RequestMapping(value = "/login",method = RequestMethod.POST )
    public String onLogin(@RequestParam("timestamp")String timestamp, @RequestParam("data")String data){
        log.info("login接口被请求，请求参数为:"+timestamp+","+data);
        Gson gson = new Gson();
        RequestDTO<LoginRequestDTO> model = gson.fromJson(data,new TypeToken<RequestDTO<LoginRequestDTO>>(){}.getType());

        ResponseDTO<LoginResponseDTO> responseModelResponseModel;
        responseModelResponseModel = userService.login(model);
        if(responseModelResponseModel==null){
            responseModelResponseModel = new ResponseDTO<>(null);
            responseModelResponseModel.setCode(0);
            responseModelResponseModel.setMessage(Constant.LOGIN_FAIL);
        }else{
            responseModelResponseModel.setCode(1);
            responseModelResponseModel.setMessage(Constant.LOGIN_SUCCESS);
        }
        return gson.toJson(responseModelResponseModel);
    }
}
